<?php
/* 
 * class.myhomeBase.php
 * 我的家园基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-24
 */
class myhomeBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
        $this->prompt = "";
    }

    //错误跳转
    function jumpError($error="", $pagetitle="", $backurl="" ){
    	$errors = $error ? $error : $this->error;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-错误提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	$content = $errors;
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //提示跳转
    function jumpPrompt($prompt="", $pagetitle="", $backurl=""){
        $prompts = $prompt ? $prompt : $this->prompt;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-信息提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $prompts);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //获取用户收藏的商家
    function getUserFavorites($userid="", $sellerid="", $count=false){
        if(!$userid && !$sellerid) return false;
        if($sellerid) $append_sid = " AND `SellerID`='".$sellerid."'";
    	if($userid) $append_uid = " AND `UserID`='".$userid."'";
    	
    	if($count){
    	    $sql = "SELECT count(`UserFavId`) FROM `fun_user_favorites` WHERE 1 " . $append_sid . $append_uid;
    	    $counts = $this->core->db->get_var($sql);
            return $counts;
        }else{
            $sql = "SELECT * FROM `fun_user_favorites` WHERE 1 " . $append_sid . $append_uid;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            return $arr_infos;
        }
    }
    
    //获取用户的自定义信息
    function getUserCustomeInfo($user_id, $count=false){
        if(!$user_id) return false;
        if($count){
            $sql = "SELECT count(`CustomID`) FROM `fun_user_custom` WHERE 1 AND `UserID`='".$user_id."'";
            $counts = $this->core->db->get_var($sql);
            return $counts;
        }else{
            $sql = "SELECT * FROM `fun_user_custom` WHERE 1 AND `UserID`='".$user_id."'";
            $res = $this->core->db->get_row($sql);
            $arr_infos = array();
		    if(!empty($res)){
                foreach($res as $k => $v){
                    $arr_infos[$k] = $v;
                }
            }
            return $arr_infos;
        }
    }
    
    //设置用户自定义信息
    function setUserCustomeInfo($user_id, $field, $value){
        if(!$user_id) return false;
        $tag = $this->getUserCustomeInfo($user_id, true);
        if($tag){//已经设置过，更新
            $sql = "UPDATE `fun_user_custom` set `".$field."`='".$value."' WHERE `UserID`='".$user_id."'";
        }else{
            $sql = "INSERT INTO `fun_user_custom` SET `UserID`='".$user_id."', `".$field."`='".$value."'";
        }
        $res = $this->core->db->query($sql);
        return $res;
    }
    
    //获取用户消息
    function getUserMsg($msg_id = "", $sender_id="", $receiver_id="", $type="", $isread="", $count=false, $arr_limit=""){
        if(!$msg_id && !$sender_id && !$receiver_id) return false;
        if($msg_id) $append_msgid = " AND `MsgID`='".$msg_id."'";

        if($sender_id) $append_sender = " AND `PosterID`='".$sender_id."'";
        if($receiver_id) $append_receiver = " AND `ReceiverID`='".$receiver_id."'";

        if($type) $append_type = " AND `Msgtype`='".$type."'";
        if($isread) $append_read = " AND `Isread`='".$isread."'";
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." , ".$arr_limit['pagesize'];
        if($count){
            $sql = "SELECT count(`MsgID`) FROM `fun_msg` WHERE 1 ".$append_msgid.$append_sender.$append_receiver.$append_type.$append_read;
            $counts = $this->core->db->get_var($sql);
            return $counts;
        }else{
            $sql = "SELECT * FROM `fun_msg` WHERE 1 ".$append_msgid.$append_sender.$append_receiver.$append_type.$append_read . " ORDER BY `Isread` DESC, `Sendtime` DESC".$append_limit;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            if($msg_id) return $arr_infos[0];
            return $arr_infos;
        }
    
    }
    
    //获取用户与朋友的所有对话消息
    function getUserFriendMsg($user_id="", $friend_id="", $isread="", $count=false, $arr_limit=""){
        if(!$user_id && !$friend_id) return false;
        
        $append_uf = " AND ( `PosterID`='$user_id' AND `ReceiverID`='$friend_id' OR `PosterID`='$friend_id' AND `ReceiverID`='$user_id' )";
        
        $append_type = " AND `Msgtype`='1'";
        
        if($isread) $append_read = " AND `Isread`='".$isread."'";
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." , ".$arr_limit['pagesize'];
        if($count){
            $sql = "SELECT count(`MsgID`) FROM `fun_msg` WHERE 1 ".$append_uf.$append_type.$append_read;
            $counts = $this->core->db->get_var($sql);
            return $counts;
        }else{
            $sql = "SELECT * FROM `fun_msg` WHERE 1 ".$append_uf.$append_type.$append_read . " ORDER BY `Sendtime` DESC".$append_limit;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            return $arr_infos;
        }
    
    }
    
    //判断是否是好友
    function checkIsFriend($userid, $friendid){
        if(!$userid || !$friendid) return false;
        $sql = "SELECT count(`UserFid`) FROM `fun_user_friend` WHERE 1 AND `UserID`='".$userid."' AND `UserFriendID`='".$friendid."'";
        $tag = $this->core->db->get_var($sql);
        return $tag;
    }
    
    //判断是否是关注人
    function checkIsAttention($user_id, $att_id){
        if(!$user_id || !$att_id) return false;
        $sql = "SELECT count(`TalkattentionID`) FROM `fun_talk_attention` WHERE 1 AND `UserID`='".$user_id."' AND `AttentionUser`='".$att_id."' AND `Del`='0'";
        $tag = $this->core->db->get_var($sql);
        return $tag;
    }
    
    //删除好友
    function delUserFriend($user_id, $friend_id){
        if(!$user_id || !$friend_id) return false;
        $sql = "DELETE FROM `fun_user_friend` WHERE `UserID`='".$user_id."' AND `UserFriendID`='".$friend_id."'";
        $res = $this->core->db->query($sql);
        return $res;
    }
    
    //添加好友
    function addUserFriend($user_id, $friend_id){
        if(!$user_id || !$friend_id) return false;
        $sql = "INSERT INTO `fun_user_friend` SET `UserID`='".$user_id."', `UserFriendID`='".$friend_id."', `Status`='0', `UserFriendInfo`='系统', `UserFriendCreateTime`=now()";
        $res = $this->core->db->query($sql);
        return $res;
    }
}
?>
